﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Baza.EF.BLL;
using Baza.EF.DAL;


namespace Pep_3014_3070_Desktop
{
    public partial class ChangeDataCustomer : Form
    {
        public ChangeDataCustomer()
        {
            InitializeComponent();
        }

        private void btnOdustani_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ChangeDataCustomers_Load(object sender, EventArgs e)
        {
            cmbKlijent.DataSource = DAKorisnici.SelectAllCustomers();
            cmbKlijent.DisplayMember = "FirstName";



        }

        private void cmbKlijent_SelectedIndexChanged(object sender, EventArgs e)
        {
            Customers customer = (Customers)cmbKlijent.SelectedItem;

            imeInput.Text = customer.FirstName;
            prezimeInput.Text = customer.LastName;
            emailInput.Text = customer.Email;


            cmbGrad.DataSource = DAIznajmljivaciApartmani.SelectCities();
            cmbGrad.DisplayMember = "Name";
            cmbGrad.SelectedIndex = (int)customer.Address.CityID;

            BrojUliceInput.Text = customer.Address.StreetNumber.ToString();
            Adresa1Input.Text = customer.Address.Address1.ToString();
            Adresa2Input.Text = customer.Address.Address2.ToString();


        }

        private void cmbKlijent_Format(object sender, ListControlConvertEventArgs e)
        {
            string lastname = ((Customers)e.ListItem).FirstName;
            string firstname = ((Customers)e.ListItem).LastName;
            e.Value = lastname + " " + firstname;
        }

        private void btnSpremi_Click(object sender, EventArgs e)
        {
            Customers customer = new Customers();

            customer.FirstName = imeInput.Text.Trim();
            customer.LastName = prezimeInput.Text.Trim();
            customer.Email = emailInput.Text.Trim();


            if (passwordInput.Text.Length > 0)
            {
                customer.PasswordSalt = UIHelper.GenerateSalt();
                customer.PasswordHash = UIHelper.GenerateHash(passwordInput.Text, customer.PasswordSalt);
            }

            bool checkedValue = false;

            if (Aktivan.Checked)
                checkedValue = true;

            customer.Status = checkedValue;

            customer.Address.CityID = cmbGrad.SelectedIndex;
            customer.Address.StreetNumber = Int32.Parse(BrojUliceInput.Text);
            customer.Address.Address1 = Adresa1Input.Text;
            customer.Address.Address2 = Adresa2Input.Text;


            DAKorisnici.UpdateCustomer(customer);

        }
    }
}
